package com.wusong.services;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.*;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioButton;
import android.widget.Toast;
import android.util.Log;

import com.wusong.services.R.id;

public class MoniterActivity extends Activity {
	private Button btn_run;
	private EditText etData;
	private EditText etIntervalTime;
	private EditText etAlertValue;
	private final String _TAG = "Moniter";
	private CheckBox cb_alertOption;
	private CheckBox cb_moniterOption;
	private RadioGroup rdgMoniterType;
	private RadioButton rdLess;

	private int _intervalTime = 1;
	private double _alertValue = -1;
	private IMoniteService moniteService;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v("test", "running");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		setElements();
		setEveentListenser();
		Intent intent = new Intent(MoniterActivity.this,GoldMonitService.class);
		this.bindService(intent,
				this.serviceConnection, BIND_AUTO_CREATE);

		
	}

	@Override
	protected void onDestroy() {
		this.unbindService(serviceConnection);
		super.onDestroy(); // ע���Ⱥ�
	}

	private ServiceConnection serviceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			moniteService = (IMoniteService) service;

		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			moniteService = null;
		}

	};

	protected void setElements() {
		btn_run = (Button) this.findViewById(R.id.btn_run);
		cb_alertOption = (CheckBox) this.findViewById(R.id.cbalertOption);
		etData = (EditText) this.findViewById(id.etData);
		cb_moniterOption = (CheckBox) this.findViewById(R.id.cbPauseMonite);
		etIntervalTime = (EditText) this.findViewById(R.id.etIntervalTime);
		etAlertValue = (EditText) this.findViewById(R.id.etalertValue);
		rdgMoniterType = (RadioGroup) this.findViewById(R.id.rdgMoniterType);
		rdLess = (RadioButton) this.findViewById(id.rdLess);
		this._intervalTime = Integer.parseInt(etIntervalTime.getText()
				.toString());
	}

	protected void setEveentListenser() {
		this.btn_run.setOnClickListener(runMoniter);
		this.cb_alertOption.setOnCheckedChangeListener(cbalertOptionHandler);
		this.cb_moniterOption.setOnCheckedChangeListener(moniterOptionHandler);
		this.rdgMoniterType.setOnCheckedChangeListener(rdgCompareTygeHandler);

	}

	protected void checkMsg() {
      String msg=moniteService.getMsg();
      if(msg!=null){
    	  etData.setText(msg);
      }
	}

	protected View.OnClickListener runMoniter = new View.OnClickListener() {

		@SuppressWarnings("null")
		@Override
		public void onClick(View v) {
			String intertTimeStr = etIntervalTime.getText().toString().trim();
			String alertValueStr = etAlertValue.getText().toString().trim();
			if (intertTimeStr != null || intertTimeStr.length() > 1) {
				_intervalTime = Integer.parseInt(intertTimeStr);
				moniteService.setAlertFreq(_intervalTime);
			}
			if (alertValueStr != null || alertValueStr.length() > 1) {
				_alertValue = Double.parseDouble(alertValueStr);
				moniteService.setAlertValue(_alertValue);
			}
		}
	};

	protected CompoundButton.OnCheckedChangeListener moniterOptionHandler = new CompoundButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {

			moniteService.setIfRun(isChecked);
			int msg = isChecked ? R.string.startMsg : R.string.pauseMsg;
			Toast.makeText(MoniterActivity.this, msg, Toast.LENGTH_LONG).show();
		}
	};

	protected CompoundButton.OnCheckedChangeListener cbalertOptionHandler = new CompoundButton.OnCheckedChangeListener() {

		@Override
		public void onCheckedChanged(CompoundButton buttonView,
				boolean isChecked) {

			int msg = isChecked ? R.string.enabledAlertMsg
					: R.string.unabledAlertMsg;
			Toast.makeText(MoniterActivity.this, msg, Toast.LENGTH_LONG).show();
			moniteService.setIfRing(isChecked);
		}
	};

	private RadioGroup.OnCheckedChangeListener rdgCompareTygeHandler = new RadioGroup.OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			moniteService.setMonitType(checkedId == rdLess.getId());
		}

	};

}